package com.zj.study.utils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.IOException;
import java.util.Properties;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

/**
 * @author 赵赳
 * @since 2022/11/30 10:32
 */
public class SFTPUtil1 {


  private static JSch jsch;
  private static Session session;


  /**
   * 连接到指定的IP
   *
   * @throws JSchException
   */
  public static void connect(String user, String passwd, String host, Integer port)
      throws JSchException {
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
    if (passwd != null) {
      session.setPassword(passwd);
    }
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftp = (ChannelSftp) channel;
  }

  public static void main(String[] args) throws JSchException {
    connect("szmcmc-user", "Mcmc#1918", "10.173.0.100", 30022);
  }

  private final static String remoteHost = "10.173.0.100";
  private final static String username = "szmcmc-user";
  private final static String password = "Mcmc#1918";


  public static SSHClient setupSshj() throws IOException {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect(remoteHost);
    client.authPassword(username, password);
    client.connect(remoteHost,30022);
    return client;
  }



}
